package com.sky.controller.user;

import com.sky.constant.RedisConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.entity.Setmeal;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user/setmeal")
@Api(tags = "C端-套餐浏览接口")
public class SetmealUserController {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 条件查询
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询套餐")
    public Result<List<Setmeal>> list(Long categoryId) {
        String CACHE_KEY = "SETMEAL_" + categoryId;
        //查询缓存
        List<Setmeal> range = (List<Setmeal>) redisTemplate.opsForValue().get(CACHE_KEY);
        if (range != null && range.size() > 0) {
            System.out.println("查询的是缓存中的套餐");
            return Result.success(range);
        }
        //查询数据库中的套餐
        SetmealDTO setmealDTO = new SetmealDTO();
        setmealDTO.setCategoryId(categoryId);
        setmealDTO.setStatus(StatusConstant.ENABLE);

        List<Setmeal> list = setmealService.list(setmealDTO);
        System.out.println("查询的是数据库中的套餐");
        //存缓存
        redisTemplate.opsForValue().set(CACHE_KEY,list);
        return Result.success(list);
    }
}
